#-------------------------------------------------------------------
# Project Invasion of the aliens 
# name game.py
# purpose main game loop
#
# authors Chris L'Heureux, Tyler Plante, Jackson Morse
# main author Chris L'Heureux
#
# created 12/3/12
# Copyright (C) Chris L'Heureux
# License New BSD\
#------------------------------------------------------------------  
import pygame, math, sys, random
from Player import Player
from Enemy1class import Enemy
from Enemy2class import Enemy2
from Enemy3class import Enemy3
from BossClass import Boss
from Gun1 import Gun
from Bullet import Bullet
from God import God
if pygame.mixer:
    pygame.mixer.init()

pygame.init()

clock = pygame.time.Clock()

screenWidth = 1024
screenHeight = 768
#test
fullscreen = 0
altFlag = False
screenSize = screenWidth, screenHeight
screen = pygame.display.set_mode((screenWidth,screenHeight),fullscreen)

bgImage = pygame.image.load("real background image.png")
bgRect = bgImage.get_rect()

GmOvimg = pygame.image.load("Game over image.png")
GmOvRect = GmOvimg.get_rect()

player = Player( 15, [500, 400])
gun = Gun()

bullets = []
maxbullets = 3

enemies = []
maxEnemies = 1
enemyOdds = 1

enemies2 = []
maxEnemies2 = 90000000
enemyOdds2 = 1

enemies3 = []
maxEnemies3 = 1
enemyOdds3 = 1




red = 0
green = 0
blue = 0
bgColor = red, green, blue
while True:
    while player.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_UP 
                    or event.key == pygame.K_w):
                        player.direction("forward") 
                elif (event.key == pygame.K_RIGHT 
                    or event.key == pygame.K_d):
                        player.direction("clockwise")
                elif (event.key == pygame.K_LEFT 
                    or event.key == pygame.K_a):
                        player.direction("counter clockwise")  
                #thanks to hellicopter rage
                elif (event.key == pygame.K_SPACE):
                        bullets += [Bullet([5,5])]                       
                elif (event.key == pygame.K_RALT or event.key == pygame.K_LALT):
                    altFlag = True
                elif (event.key == pygame.K_RETURN) and altFlag:
                    if fullscreen == 0:
                        fullscreen = pygame.FULLSCREEN
                    else:
                        fullscreen = 0
                    screen = pygame.display.set_mode((screenWidth,screenHeight),fullscreen)
                    pygame.display.flip()

            elif event.type == pygame.KEYUP:
                if (event.key == pygame.K_UP 
                    or event.key == pygame.K_w):
                        player.direction("stop")
                elif (event.key == pygame.K_RIGHT 
                    or event.key == pygame.K_d):
                        player.direction("stop clockwise")
                elif (event.key == pygame.K_LEFT 
                    or event.key == pygame.K_a):
                        player.direction("stop counter clockwise")
                elif (event.key == pygame.K_RALT or event.key == pygame.K_LALT):
                    altFlag = False
                                                         
        #print altFlag, fullscreen              

        for bullet in bullets:
            bullet.move()
          

    #------------------Enemy-spawn-code--------------------                
        if len(enemies) < maxEnemies:
            if random.randint(1, enemyOdds) == 1:
                if random.randint(1,2) == 1:
                   enemyX = random.randint(10, screenWidth+100)
                   enemyY = random.randint(0,10) * screenHeight+50
                else: 
                   enemyY = random.randint(0, screenHeight-50)
                   enemyX = random.randint(0,1) * screenWidth-75
                enemies += [Enemy([enemyX, enemyY])]
        for enemy in enemies:
            enemy.move()
            enemy.chase(player)
            enemy.collideWall(screenWidth, screenHeight)
            enemy.collidePlayer(player)
            for enemy2 in enemies2:
                enemy.collideEnemy(enemy2)
            for enemy3 in enemies3:
                enemy.collideEnemy(enemy3)
            if not enemy.living:
                enemies.remove(enemy)
            enemy.move()
                
        for first in range(0,len(enemies)-2):
            for second in range(first+1, len(enemies)-1):
                enemies[first].collideEnemy(enemies[second])
            
        
        if len(enemies2) < maxEnemies2:
            if random.randint(1, enemyOdds2) == 1:
                if random.randint(1,2) == 1:
                   enemyX2 = random.randint(10, screenWidth+100)
                   enemyY2 = random.randint(0,10) * screenHeight+50
                else: 
                   enemyY2 = random.randint(0, screenHeight-50)
                   enemyX2 = random.randint(0,1) * screenWidth-75
                enemies2 += [Enemy2([enemyX2, enemyY2])]
        for enemy2 in enemies2:
            enemy2.move() 
            enemy2.collideWall(screenWidth, screenHeight)
            enemy2.collidePlayer(player)
            for enemy3 in enemies3:
                enemy2.collideEnemy(enemy3)
            if not enemy2.living:
                enemies2.remove(enemy2)     
            enemy2.move()
            
        for first in range(0,len(enemies2)-2):
            for second in range(first+1, len(enemies2)-1):
                enemies2[first].collideEnemy(enemies2[second])
            
        if len(enemies3) < maxEnemies3:
            if random.randint(1, enemyOdds3) == 1:
                if random.randint(1,2) == 1:
                   enemyX3 = random.randint(10, screenWidth+100)
                   enemyY3 = random.randint(0,1) * screenHeight+50
                else: 
                   enemyY3 = random.randint(0, screenHeight-50)
                   enemyX3 = random.randint(0,1) * screenWidth-75
                enemies3 += [Enemy3([enemyX3, enemyY3])]
        for enemy3 in enemies3:
            enemy3.move() 
            enemy3.chase(player)
            #enemy3.EnemyCollide(enemy)
            enemy3.collideWall(screenWidth, screenHeight)
            enemy3.collidePlayer(player)  
            if not enemy3.living:
                enemies3.remove(enemy3)  
            enemy3.move()
             
        for first in range(0,len(enemies3)-2):
            for second in range(first+1, len(enemies3)-1):
                enemies3[first].collideEnemy(enemies3[second]) 
        
        player.move()
        
        


        screen.blit(bgImage, bgRect)
        screen.blit(player.surface, player.rect)
        for enemy in enemies:
            screen.blit(enemy.surface, enemy.rect) 
        for enemy2 in enemies2:
            screen.blit(enemy2.surface, enemy2.rect)
        for enemy3 in enemies3:
            screen.blit(enemy3.surface, enemy3.rect)
        for bullet in bullets:
            screen.blit(bullet.surface, bullet.rect)    
        pygame.display.flip()
        clock.tick(30)
        print clock.get_fps()
        
    while not player.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN:
                if (event.key == pygame.K_RETURN):
                    player.retry("yes", [500, 400])

                    #player = Player( 10, [500, 400])
                    gun = Gun()
                    bullet = Bullet(1)

                    enemies = []
                    maxEnemies = 5
                    enemyOdds = 1

                    enemies2 = []
                    maxEnemies2 = 10
                    enemyOdds2 = 1

                    enemies3 = []
                    maxEnemies3 = 0
                    enemyOdds3 = 1


            
        screen.blit(GmOvimg, GmOvRect)
        pygame.display.flip()
            
     